<template>
	<view class="content">
		<mescroll-body ref="mescrollRef" @init="mescrollInit" :down="downOption" @down="downCallback" @up="upCallback">	
			<view class="group" v-for="(item,index) in addressList" :key="index" @click="selectAddress(item,addressType)">
				<view class="cont_top">
					<view class="cont_name">{{item.realName}}</view>
					<view class="cont_phone">{{item.phone}}</view>
				</view>
				<view class="group_cont">{{item.province}}{{item.city}}{{item.district}}{{item.detail}}</view>
				<view class="group_bot">
					<view class="group_bot_left">
						<image v-if="item.isDefault == 1" class="leftimg" src="https://taobaoke-paimai.oss-cn-beijing.aliyuncs.com/chose.png" mode=""></image>
						<image v-else @tap.stop="setMoren(item)" class="leftimg" src="https://taobaoke-paimai.oss-cn-beijing.aliyuncs.com/chose.png" mode=""></image>
						<view class="">{{item.isDefault == 1 ? '已设为默认' : '设为默认'}}</view>
					</view>
					<view class="editor" @click.stop="editorAddr(item)">编辑</view>
				</view>
			</view>
		</mescroll-body>
		
		<!-- #ifdef H5 || MP-WEIXIN -->
		<!-- 添加新地址 -->
		<view class="black"></view>
		<view class="fixed_bot">
			<view class="addbot" @click="goAdd">添加</view>
		</view>
		<!-- #endif -->
	</view>
</template>

<script>
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin],
		data() {
			return {
				selectinfo:{},//地址信息
				downOption: {
					auto: true //是否在初始化后,自动执行downCallback; 默认true
				},
				upOption: {
					auto: true, // 不自动加载
					// page: {
					// 	num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
					// 	size: 10 // 每页数据的数量
					// },
					noMoreSize: 4, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
					empty: {
						tip: '~ 空空如也 ~', // 提示
						btnText: '刷新',
						// 列表第一页无任何数据时,显示的空布局
						// use : 是否启用
						// icon : 空布局的图标路径 (支持网络路径)
						// tip : 提示文本
						// btnText : 按钮文本
						// fixed : 是否使用定位,默认false; 配置fixed为true,以下的top和zIndex才生效 (transform会使fixed失效,所以最终还是会降级为absolute)
						// top : fixed定位的top值 (完整的单位值,如"100rpx", "10%";)
						// zIndex : fixed定位z-index值
					},
					textNoMore: "--我也是有底线的--" //没有更多数据的提示文本
				},
				addressList:[],
				addressType:''  // 根据type判断是从哪里跳转
				
			}
		},
		onLoad(options) {
			let that = this;
			if (Object.keys(options).length != 0) {
				that.addressType = options.addressType
			}
			uni.$on('address',function(data){
				console.log('监听到事件来自 id ，携带参数 msg 为：' + data.msg);
				that.mescroll.resetUpScroll();
			})
		},
		methods: {
			selsetaddress(item, index) { //选择地址
				this.curadd = index;
				this.selectinfo = item;
				this.selectAddr()
			},
			selectAddr() { //确认选择
			    uni.$emit('setaddress', this.selectinfo)
				uni.navigateBack({
					delta: 1,
				})
				
			},
			downCallback(){
				this.mescroll.resetUpScroll(); //调用加载
			},
			upCallback(page) {
				//联网加载数据
				console.log(page)
				let that = this;
				that.api.userAddressList({
					page:page.num,
					limit:page.size
				}).then(res => {
					console.log(res)
					
					let curPageData =res.data.records;
					// 接口返回的当前页数据长度 (如列表有26个数据,当前页返回8个,则curPageLen=8)
					let curPageLen = curPageData.length;
					// 接口返回的总页数 (如列表有26个数据,每页10条,共3页; 则totalPage=3)
					let totalPage = res.data.pages;
					//设置列表数据
					setTimeout(() => { // 模拟效果
						if (page.num == 1) this.addressList = []; //如果是第一页需手动置空列表
						this.addressList = this.addressList.concat(curPageData); //追加新数据
						console.log(this.addressList,"地址信息");
						// 请求成功,隐藏加载状态
						//方法一(推荐): 后台接口有返回列表的总页数 totalPage
						this.mescroll.endByPage(curPageLen, totalPage);
					}, 500)
					
					
				})
				
				
			},
			// 新增地址
			onNavigationBarButtonTap: function (e) {
				uni.navigateTo({
					url: 'addAddress/addAddress',
				});
			},
			// 设为默认
			setMoren: function (item) {
				let that = this;
				console.log(item)
				let isDefault = item.isDefault;
				if (isDefault == 1) {
					isDefault = 0;
				} else if (isDefault == 0) {
					isDefault = 1
				}
				that.api.userSetDefaultAddress({
					id:item.id
				}).then(res => {
					console.log(res)
					that.tui.toast('设置成功');
					setTimeout(function () {
						uni.$emit('address',{msg:'更新数据'})
					},500)
				})
				
			},
			// 编辑
			editorAddr: function (item) {
				uni.navigateTo({
					url: 'editorAddress/editorAddress?id=' + item.id,
				});
			},
			// 选择地址
			selectAddress: function (item,addressType) {
				if (addressType == 1) {
					var pages = getCurrentPages();
					var currPage = pages[pages.length - 1]; //当前页面
					var prevPage = pages[pages.length - 2]; //上一个页面
					prevPage.$vm.userAddressId = item.id;
					setTimeout(function () {
						uni.$emit('updateAddress',{msg:'更新数据'})
						uni.navigateBack({
							delta: 1
						});
					},1000)
				} else if (addressType == 2) {
					var pages = getCurrentPages();
					var currPage = pages[pages.length - 1]; //当前页面
					var prevPage = pages[pages.length - 2]; //上一个页面
					prevPage.$vm.userAddressId = item.id;
					setTimeout(function () {
						uni.$emit('giftAddress',{msg:'切换地址'})
						uni.navigateBack({
							delta: 1
						});
					},1000)
				}
			},
			// 添加
			goAdd: function () {
				uni.navigateTo({
					url: 'addAddress/addAddress',
				});
			}
			
		}
	}
</script>

<style lang="scss">
	page {
		width: 100%;
		box-sizing: border-box;
		letter-spacing: 3upx;
		background: #F5F5F5;
		padding: 20upx 0;
		color: #333333;
	}
	.content {
		width: 100%;
		box-sizing: border-box;
		background: #F5F5F5;
	}
	.group {
		width: 100%;
		box-sizing: border-box;
		background: #FFFFFF;
		padding: 20upx 30upx;
		margin-bottom: 20upx;
	}
	.cont_top {
		width: 100%;
		box-sizing: border-box;
		display: flex;
		justify-content: flex-start;
		align-items: center;
		line-height: 60upx;
		font-size: 32upx;
		.cont_name {
			margin-right: 50upx;
		}
	}
	
	.group_cont {
		width: 100%;
		box-sizing: border-box;
		font-size: 24upx;
		color: #666666;
		padding: 10upx 0 20upx 0;
	}
	.group_bot {
		width: 100%;
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;
		padding: 20upx 0;
		border-top: 1upx solid #F8F8F8;
	}
	.group_bot_left {
		width: 50%;
		box-sizing: border-box;
		display: flex;
		justify-content: flex-start;
		align-items: center;
		font-size: 24upx;
		color: #666666;
		.leftimg {
			width: 32upx;
			height: 32upx;
			margin-right: 15upx;
		}
	}
	.editor {
		width: 70upx;
		height: 50upx;
		background: #7234BD;
		color: #FFFFFF;
		text-align: center;
		line-height: 50upx;
		font-size: 22upx;
		border-radius: 10rpx;
	}
	.black {
		width: 100%;
		box-sizing: border-box;
		height: 150rpx;
	}
	.fixed_bot {
		width: 100%;
		box-sizing: border-box;
		background: #FFFFFF;
		padding: 10rpx 85rpx;
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 100;
	}
	.addbot {
		width: 100%;
		box-sizing: border-box;
		background: #E42221;
		color: #FFFFFF;
		text-align: center;
		line-height: 80rpx;
		border-radius: 10rpx;
	}
	
	
	

</style>
